/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.Ventas.Vista;

import java.awt.event.ActionListener;
import java.awt.event.KeyListener;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.table.DefaultTableModel;
import pe.edu.pucp.Modelo.Cliente;
import pe.edu.pucp.Modelo.Permiso;
import pe.edu.pucp.Modelo.TipoDocumento;
import pe.edu.pucp.Util.Constante;
import pe.edu.pucp.Util.ValHelper;

/**
 *
 * @author katy
 */
public class BuscarClienteGUI extends javax.swing.JPanel {

    /**
     * Creates new form ClienteGUI
     */
    private JFrame padre;
    public BuscarClienteGUI(JFrame parent) {
        initComponents();
        padre = parent;
    }

     public JFrame getPadre() {
        return this.padre;
    }
    
     public String getNombresRazonSocial() {
         return this.txtNombres.getText();
     }
     
     public int getTipoDocumento() {
         return this.cmbTipoDoc.getSelectedIndex();
     }
     
     public long getNumeroDocumento() {
         long numeroDoc = 0;
         if(!this.txtNumDoc.getText().isEmpty())
             numeroDoc = Long.parseLong(this.txtNumDoc.getText());
         return numeroDoc;
     }
     
    public void mostrarPermisos(ArrayList<Permiso> list){            
            if (!list.isEmpty()){                                
                btnBuscar.setEnabled(buscarPermiso(list,Constante.P_ENV_CLI_BUSCAR)==1);
                btnCrear.setEnabled(buscarPermiso(list,Constante.P_ENV_CLI_REGISTRAR)==1);
                btnEditar.setEnabled(buscarPermiso(list,Constante.P_ENV_CLI_EDITAR)==1);
            }
    }    
  
    public int buscarPermiso(ArrayList<Permiso> list, int idel){
        int per=-1;
        for (Permiso c : list){
            if (c.getElemento().getIdelemento()==idel){ per =c.getPermiso(); }
        }
        return per;
    }     
         
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanel1 = new javax.swing.JPanel();
        btnCrear = new javax.swing.JButton();
        btnEditar = new javax.swing.JButton();
        btnBuscar = new javax.swing.JButton();
        btnLimpiar = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtNumDoc = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        txtNombres = new javax.swing.JTextField();
        cmbTipoDoc = new javax.swing.JComboBox();
        btnGenerarEnvio = new javax.swing.JButton();
        pnClientes = new javax.swing.JPanel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tblResultados = new javax.swing.JTable();

        setBorder(javax.swing.BorderFactory.createTitledBorder(""));
        setMaximumSize(new java.awt.Dimension(764, 700));
        setMinimumSize(new java.awt.Dimension(764, 700));
        setPreferredSize(new java.awt.Dimension(764, 700));

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder("Buscar"));
        jPanel1.setMaximumSize(new java.awt.Dimension(636, 240));
        jPanel1.setMinimumSize(new java.awt.Dimension(636, 240));
        jPanel1.setName(""); // NOI18N
        jPanel1.setPreferredSize(new java.awt.Dimension(634, 240));

        btnCrear.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/plus.png"))); // NOI18N
        btnCrear.setMaximumSize(new java.awt.Dimension(75, 29));
        btnCrear.setMinimumSize(new java.awt.Dimension(75, 29));
        btnCrear.setPreferredSize(new java.awt.Dimension(75, 29));

        btnEditar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/edit.png"))); // NOI18N
        btnEditar.setMaximumSize(new java.awt.Dimension(75, 29));
        btnEditar.setMinimumSize(new java.awt.Dimension(75, 29));
        btnEditar.setPreferredSize(new java.awt.Dimension(75, 29));

        btnBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/search.png"))); // NOI18N
        btnBuscar.setMaximumSize(new java.awt.Dimension(75, 29));
        btnBuscar.setMinimumSize(new java.awt.Dimension(75, 29));
        btnBuscar.setPreferredSize(new java.awt.Dimension(75, 29));

        btnLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/refresh.png"))); // NOI18N

        jLabel1.setText(" Tipo Documento :");

        jLabel2.setText("Número Documento :");

        jLabel3.setText("Nombres/Razón Social :");

        btnGenerarEnvio.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Recurso/checkmark.png"))); // NOI18N
        btnGenerarEnvio.setToolTipText("Generar Envio");

        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnEditar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addComponent(btnGenerarEnvio, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel3)
                            .addComponent(jLabel1)
                            .addComponent(jLabel2))
                        .addGap(26, 26, 26)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(txtNombres, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                            .addComponent(cmbTipoDoc, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, 164, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(0, 193, Short.MAX_VALUE)))
                .addContainerGap())
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNombres, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(cmbTipoDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(txtNumDoc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addGap(28, 28, 28)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(btnBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(btnEditar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(btnLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(btnCrear, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(btnGenerarEnvio, javax.swing.GroupLayout.PREFERRED_SIZE, 44, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pnClientes.setBorder(javax.swing.BorderFactory.createTitledBorder("Clientes"));

        tblResultados.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "NUM. DOC.", "NOMBRES", "FECHA REGISTRO", "ESTADO"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false, true
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tblResultados.getTableHeader().setReorderingAllowed(false);
        tblResultados.setUpdateSelectionOnSort(false);
        jScrollPane2.setViewportView(tblResultados);

        javax.swing.GroupLayout pnClientesLayout = new javax.swing.GroupLayout(pnClientes);
        pnClientes.setLayout(pnClientesLayout);
        pnClientesLayout.setHorizontalGroup(
            pnClientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnClientesLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 700, Short.MAX_VALUE)
                .addContainerGap())
        );
        pnClientesLayout.setVerticalGroup(
            pnClientesLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnClientesLayout.createSequentialGroup()
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 429, Short.MAX_VALUE)
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jPanel1, javax.swing.GroupLayout.DEFAULT_SIZE, 736, Short.MAX_VALUE)
                    .addComponent(pnClientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 200, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnClientes, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents
    public void addBuscarButtonListener(ActionListener listener) {
        this.btnBuscar.addActionListener(listener);
    }
    
    public void addEditarButtonListener(ActionListener listener) {
        this.btnEditar.addActionListener(listener);
    }
    
    public void addNuevoClienteButtonListerner(ActionListener  listener) {
        this.btnCrear.addActionListener(listener);
    }
    
    public void addLimpiarButtonListener(ActionListener listener) {
        this.btnLimpiar.addActionListener(listener);
    }
    
    public void addNuevoEnvioListerner(ActionListener listener) {
        this.btnGenerarEnvio.addActionListener(listener);
    }
    
    public void addSeleccionarClienteTableListener(MouseListener listener) {
        this.tblResultados.addMouseListener(listener);
    }
    
    public void addSoloTextoListener (KeyListener listener) {
        this.txtNombres.addKeyListener(listener);
    }
    
    public void addSoloNumerosListener(KeyListener listener) {
        this.txtNumDoc.addKeyListener(listener);
    }
    
    public int getSelectedClient() {
        return tblResultados.getSelectedRow();
    }
    
    public void llenaTipoDocs(ArrayList<TipoDocumento> lst) {
        
        if(lst != null)
        {
            for (TipoDocumento c : lst)
            {
                cmbTipoDoc.addItem(c.getDescripcion());
            }
        }
    }
    
    public void llenaGrilla(ArrayList<Cliente> lista){      
        if(lista!=null){ 
            ((DefaultTableModel) this.tblResultados.getModel()).setRowCount(0);
            for (int f = 0; f<lista.size();f++){
                ((DefaultTableModel) tblResultados.getModel()).setRowCount(tblResultados.getRowCount()+1);
               
                tblResultados.setValueAt(lista.get(f).getNumdoc(),f,0);
                tblResultados.setValueAt(lista.get(f).getNombre() + " "+ lista.get(f).getAppaterno(),f,1);
                tblResultados.setValueAt(lista.get(f).getFechareg().toString(),f,2);
                tblResultados.setValueAt(lista.get(f).getEstadoCliente().getDescripcion(), f, 3);
             }
            pnClientes.setBorder(javax.swing.BorderFactory.createTitledBorder("Clientes ("+lista.size() + ")"));            
        }
        else {
            pnClientes.setBorder(javax.swing.BorderFactory.createTitledBorder("Clientes"));            
        }                
    }
     
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnBuscar;
    private javax.swing.JButton btnCrear;
    private javax.swing.JButton btnEditar;
    private javax.swing.JButton btnGenerarEnvio;
    private javax.swing.JButton btnLimpiar;
    private javax.swing.JComboBox cmbTipoDoc;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JPanel pnClientes;
    private javax.swing.JTable tblResultados;
    private javax.swing.JTextField txtNombres;
    private javax.swing.JTextField txtNumDoc;
    // End of variables declaration//GEN-END:variables
}
